package com.tmc.web.controller.inthotel;

import java.io.UnsupportedEncodingException;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.tmc.bean.inthotel.HotelBaseInfo;
import com.tmc.bean.inthotel.TouricoResult;
import com.tmc.bean.inthotel.hotel.City;
import com.tmc.util.DateUtil;
import com.tmc.vendor.inthotel.tourico.service.CityService;
import com.tmc.vendor.inthotel.tourico.service.HotelService;
import com.tourico.schemas.webservices.hotelv3.ArrayOfChildAge;
import com.tourico.schemas.webservices.hotelv3.ArrayOfRoomInfo;
import com.tourico.schemas.webservices.hotelv3.ChildAge;
import com.tourico.schemas.webservices.hotelv3.Hotel;
import com.tourico.schemas.webservices.hotelv3.RoomInfo;
import com.tourico.schemas.webservices.hotelv3.SearchRequest;

@Controller
@RequestMapping("/hotelList")
public class HotelListController {
	
	private Log logger = LogFactory.getLog(this.getClass());
	
	@Autowired
	private HotelService hotelService;
	@Autowired
	private CityService cityService;
	private List<Hotel> hotels;
	
	@RequestMapping(value="hotel_list",method = RequestMethod.GET)
	public ModelAndView HotelList(){
		Map<String, Object> map = new HashMap<String, Object>();
		return new ModelAndView("views/search/hotelSearch", map);
	}
	
	@RequestMapping(value = "/list")
	public ModelAndView findList(@RequestParam(required=true,value="city")String city,
								 @RequestParam(required=false,value="hotel")String hotel,	
								 @RequestParam(required=false,value="hotelCityName")String hotelCityName,
								 @RequestParam(required=false,value="hotelLocationName")String hotelLocationName,
								 @RequestParam(required=false,value="hotelName")String hotelName,
								 @RequestParam(required=true,value="checkIn")String checkIn,
								 @RequestParam(required=true,value="checkOut")String checkOut,
								 @RequestParam(required=true,value="adultNum")Integer adultNum,
								 @RequestParam(required=false,value="childNum")Integer childNum,
								 @RequestParam(required=false,value="childAge")Integer childAge,
								 @RequestParam(required=false,value="maxPrice")Integer maxPrice,
								 @RequestParam(required=false,value="starLevel")Integer starLevel,
								 @RequestParam(required=false,value="availableOnly")String availableOnly,
								 @RequestParam(required=false,value="propertyType")String propertyType,
								 @RequestParam(required=false,value="exactDestination")String exactDestination,
								 @RequestParam(required=false,value="pageIndex")Integer pageIndex,
								 @RequestParam(required=false,value="pageSize")Integer pageSize){
		System.out.println("开始进入方法中。。。");
		if(pageIndex==null){
			pageIndex = 1;
		}
		if(pageSize==null){
			pageSize = 10;
		}
		Map<String, Object> map = new HashMap<String, Object>();
		List<City> cities = new ArrayList<City>();
		try {
			city = new String(city.getBytes("iso8859-1"),"UTF-8");
			if(hotel!=null){
				hotel = new String(hotel.getBytes("iso8859-1"),"UTF-8");
			}
		} catch (UnsupportedEncodingException e1) {
			e1.printStackTrace();
		}
		TouricoResult tr = new TouricoResult();
		try {
			SearchRequest request = new SearchRequest();
			City c = new City();
			c.setCityName(city);
			cities = cityService.findCity(c);
			request.setDestination(cities.get(0).getCityCode());
			//request.setDestination(destination);
			request.setHotelCityName(hotelCityName);
			request.setHotelLocationName(hotelLocationName);
			request.setHotelName(hotelName);
			request.setCheckIn(DateUtil.XMLGregorianCalendarFormat(checkIn,"yyyy-MM-dd"));
			request.setCheckOut(DateUtil.XMLGregorianCalendarFormat(checkOut,"yyyy-MM-dd"));
			//灏佽ArrayOfRoomInfo
			ArrayOfRoomInfo arrRoomInfo = new ArrayOfRoomInfo();
			List<RoomInfo> roomInfolist = new ArrayList<RoomInfo>();
			RoomInfo roomInfo = new RoomInfo();
			roomInfo.setAdultNum(adultNum);
			roomInfo.setChildNum(childNum);
			//灏佽ArrayOfChildAge
			ArrayOfChildAge arrChildAge = new ArrayOfChildAge();
			List<ChildAge> childAgelist = new ArrayList<ChildAge>();
			ChildAge childAgeInfo = new ChildAge();
			childAgeInfo.setAge(childAge);
			childAgelist.add(childAgeInfo);
			arrChildAge.setChildAge(childAgelist);
			roomInfo.setChildAges(arrChildAge);
			roomInfolist.add(roomInfo);
			arrRoomInfo.setRoomInfo(roomInfolist);
			request.setRoomsInformation(arrRoomInfo);
			if(maxPrice!=null){
				request.setMaxPrice(new BigDecimal(maxPrice));
			}
			if(starLevel!=null){
				request.setStarLevel(new BigDecimal(starLevel));
			}
			request.setAvailableOnly(Boolean.getBoolean(availableOnly));
			request.setExactDestination(Boolean.getBoolean(exactDestination));
			List<HotelBaseInfo> baseInfos = hotelService.searchHotels(request,null);
			map.put("request",request);
			map.put("pageSize",pageSize);
			map.put("pageIndex",pageIndex);
			
			/*map.put("hotels",resp.getSearchHotelsResult().getHotelList().getHotel());
			map.put("hotel", resp.getSearchHotelsResult().getHotelList().getHotel().get(0));
			map.put("rooms", resp.getSearchHotelsResult().getHotelList().getHotel().get(0).getRoomTypes().getRoomType());
			System.out.println(resp.getSearchHotelsResult().getHotelList().getHotel().get(0).getLocation().getLatitude());
			System.out.println(resp.getSearchHotelsResult().getHotelList().getHotel().get(0).getLocation().getLongitude());
			System.out.println(resp.getSearchHotelsResult().getHotelList().getHotel().get(0).getThumb());*/
			//map.put("city",cities.get(0));
			//return new ModelAndView("views/search/hotelSearch",map);
			//return new ModelAndView("views/content/detail",map);
			return new ModelAndView("views/search/hotelSearch", map);
		} catch (Exception e) {
			e.printStackTrace();
			tr.makeFail("请求失败");
			//return JSON.toJSONString(tr);
			return null;
		} 
	}
	
	@RequestMapping(value = "/detial",method = RequestMethod.POST)
	public ModelAndView findHotelDetail(@RequestParam(required=true,value="hotelId")Integer hotelId){
		Map<String,Object> map = new HashMap<String,Object>();
		Hotel hotel = new Hotel();
		hotel.setHotelId(hotelId);
		int index = hotels.indexOf(hotel);
		map.put("hotel", hotels.get(index));
		map.put("rooms", hotels.get(index).getRoomTypes().getRoomType());
		System.out.println(hotels.get(index).getLocation().getLatitude());
		System.out.println(hotels.get(index).getLocation().getLongitude());
		return new ModelAndView("views/content/detail",map);
	}
	
}
